#!/bin/zsh
set -euo pipefail
RES="$(cd "$(dirname "$0")/../Resources" && pwd)"
PY=""
for c in /opt/homebrew/bin/python3 /usr/local/bin/python3 /usr/bin/python3 python3; do
  if command -v "$c" >/dev/null 2>&1; then
    if "$c" -c "import sys; raise SystemExit(0 if sys.version_info >= (3,10) else 1)" 2>/dev/null; then
      PY="$(command -v "$c")"
      break
    fi
  fi
done
if [[ -n "$PY" ]] && "$PY" -c "import tkinter" 2>/dev/null; then
  exec "$PY" "$RES/mac_installer_gui.py"
elif [[ -n "$PY" ]]; then
  osascript -e 'display notification "Installing Drift Snip..." with title "Drift Snip"' || true
  "$PY" -c "import sys; sys.path.insert(0, '$RES'); from mac_installer_gui import perform_mac_install; ok, err = perform_mac_install(install_finder_action=True, on_progress=lambda p,m: None); raise SystemExit(0 if ok else 1)"
  open "$HOME/Applications/Drift Snip.app"
else
  osascript -e 'display dialog "Python 3.10+ is required to install Drift Snip.\n\nInstall via Homebrew (brew install python) or python.org, then double-click Install Drift Snip again." buttons {"OK"} default button "OK" with title "Drift Snip Setup"'
fi
